package it.softphone.cmc.server.entity;

import it.softphone.cmc.shared.entity.Attributes;
import it.softphone.cmc.shared.entity.Tenant;
import it.softphone.commons.json.shared.JSONTransient;
import it.softphone.commons.service.LogAware;

import java.io.Serializable;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.OneToOne;
import javax.persistence.Transient;

import com.google.appengine.api.datastore.Blob;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

@SuppressWarnings("serial")
@Entity
public class TenantEntity implements Serializable, Tenant, LogAware {

	@Id @GeneratedValue(strategy = GenerationType.IDENTITY) 
	@JSONTransient private Key GUID;
	
	private String id1;
	private String id2;
	private String id3;
	
	
	private String name;
	@Embedded 
	@OneToOne(cascade=CascadeType.ALL)
	private AttributesEntity attributes = new AttributesEntity();
	
	
	@Lob private Blob logo;
	
	/// NEED ONLY FOR TRANSIENT DECLARATION
	@JSONTransient 
	@Transient 
	private byte[] logoB;
	@Transient 
	protected final List<String> UID = null;
	

	@Override
	public String toString() {
		return String
				.format("TenantEntity [id1=%s, id2=%s, id3=%s, name=%s, attributes=%s, UID=%s]",
						id1, id2, id3, name, attributes, getGUID());
	}

	public final Key getGUID() {
		return GUID;
	}

	public final void setGUID(Key uID) {
		GUID = uID;
	}

	@Override
	public String getId1() {
		return id1;
	}


	@Override
	public void setId1(String id1) {
		this.id1 = id1;
	}


	@Override
	public String getId2() {
		return id2;
	}


	@Override
	public void setId2(String id2) {
		this.id2 = id2;
	}


	@Override
	public String getId3() {
		return id3;
	}


	@Override
	public void setId3(String id3) {
		this.id3 = id3;
	}


	/* (non-Javadoc)
	 * @see it.softphone.cmc.entity.Tenant#getName()
	 */
	@Override
	public final String getName() {
		return name;
	}

	/* (non-Javadoc)
	 * @see it.softphone.cmc.entity.Tenant#setName(java.lang.String)
	 */
	@Override
	public final void setName(String name) {
		this.name = name;
	}

	//@JSONTransient cause a java.lang.VerifyError
	/* (non-Javadoc)
	 * @see it.softphone.cmc.entity.Tenant#getLogo()
	 */
	public final Blob getLogo() {
		return logo;
	}

	/* (non-Javadoc)
	 * @see it.softphone.cmc.entity.Tenant#setLogo(com.google.appengine.api.datastore.Blob)
	 */
	public final void setLogo(Blob logo) {
		this.logo = logo;
	}

	@Override
	public byte[] getLogoB() {
		return getLogo().getBytes();
	}


	@Override
	public void setLogoB(byte[] bytes) {
		setLogo( new Blob(bytes) );
	}
	
	@Override
	public void setUID(String value) {
		setGUID( KeyFactory.stringToKey(value) );
	}


	@Override
	public String getUID() {
		if( getGUID()==null) return null;
		return KeyFactory.keyToString(getGUID());
	}
	
	@Override
	public Attributes getAttributes() {
		return attributes;
	}


	@Override
	public void setAttributes(Attributes value) {
		attributes = (AttributesEntity) value;
		
	}


	@SuppressWarnings("unchecked")
	@Override
	public it.softphone.commons.json.server.JSONObject encodeAttributes() {	
		return  attributes.encodeAttributes();
	}

	@Override
	public void decodeAttributes(Object object) {
		attributes.decodeAttributes(object);
	}

	
}
